﻿using Newtonsoft.Json;
using System;
using System.Data;
using System.Text;
using System.Web.Services;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal.CadreWork
{
    public partial class Suggestion : UseMorePageMasterPage
    {
        #region 页面属性
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }
        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string Search
        {
            get
            {
                return this.Request["Search"] ?? "";
            }
        }

        private string Status
        {
            get
            {
                return this.Request["Status"] ?? "";
            }
        }

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }

            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();
            }
        }

        private void LoadData()
        {
            btnSave.Enabled = btnReset.Enabled = IsLogin;
        }

        private void WriteList()
        {
            StringBuilder sbHtml = new StringBuilder();
            DataTable dt = PartyCadresWorkSuggest.GetPortalListData(Convert.ToInt32(this.PageIndex), Convert.ToInt32(PageSize), this.Search, this.Status).Tables[0];

            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(PartyCadresWorkSuggest.GetPortalListData(0, Convert.ToInt32(PageSize), this.Search, this.Status).Tables[1].Rows[0][0].ToString());
            Response.End();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string title = txtTitle.Text.Trim();
            string userName = txtUserName.Text.Trim();
            string content = txtMobileNo.Text.Trim();

            PartyCadresWorkSuggest suggest = new PartyCadresWorkSuggest();
            suggest.ID = StringHelper.NewID;
            suggest.Title = title;
            suggest.UserName = userName;
            suggest.ContactWay = content;
            suggest.Status = 0;
            suggest.Content = txtContent.Text.Trim();
            suggest.IsVisible = true;
            suggest.CreateTime = DateTime.Now;
            suggest.CreateBy = CurrentUser.LoginID;
            suggest.ModifyTime = DateTime.Now;
            suggest.ModifyBy = CurrentUser.LoginID;

            DB.PartyCadresWorkSuggests.Add(suggest);
            DB.SaveChanges();

            Response.Redirect(Request.Url.ToString());
        }

        [WebMethod]
        public static string IsContainSensitiveWords(string content)
        {
            return KeyWord.IsContainSensitiveWords(content) ? "success" : "fail";
        }
    }
}